package com.galeno.redis;

import org.apache.commons.lang3.RandomStringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPubSub;

import java.util.Locale;

/**
 * @author galeno
 * @Title:
 * @Description:
 * @date 2021/9/1422:51
 */
public class RedisPubSubDemo {
    public static void main(String[] args) {
        new Thread(() -> {
            Jedis jedis = new Jedis("galeno01", 6379);
            //订阅频道
             jedis.subscribe(new JedisPubSub() {
                 @Override
                 public void onMessage(String channel, String message) {
                     System.out.println(channel+message);
                 }
             },"ok");


        }).start();


        new Thread(() -> {
            Jedis jedis = new Jedis("galeno01", 6379);
            for (int i = 0; i <Integer.MAX_VALUE; i++) {
                jedis.publish("ok", RandomStringUtils.randomAlphabetic(3,6).toUpperCase(Locale.ROOT));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }


            }


        }).start();


    }
}
